Skip to content

Warning 20 range: also walk through let/use bindings (follow-up to #19504)#19896

Open
T-Gro wants to merge 1 commit into
mainfrom
copilot/fix-warning-20-let-binding-range
Open

Warning 20 range: also walk through let/use bindings (follow-up to #19504)#19896
T-Gro wants to merge 1 commit into
mainfrom
copilot/fix-warning-20-let-binding-range

Conversation

@T-Gro

@T-Gro T-Gro commented Jun 5, 2026

Copy link
Copy Markdown
Member

Follow-up to #19504 addressing review feedback from @auduchinok.

#19504 fixed warning 20 ("expression is implicitly ignored") to point at the last expression of a sequential body inside for/while. The walk only descended through SynExpr.Sequential, so when the last expression was the body of a let/use binding, the squiggle covered the whole let expression instead of just the offending tail:

for _ in [] do
    let x = 1   // \
    x           //  > squiggle spanned both lines (from `let` to end of `x`)

This PR also descends through SynExpr.LetOrUse, so the squiggle lands on x alone in cases like the one above.

It also corrects the issue reference cited by the original PR (#5418 — "Wrong expression is ignored warning range"; the previously cited #5735 is unrelated to this diagnostic).

Tracking: #5418

- Walk through let/use bindings in addition to Sequential when locating
  the offending expression for warning 20. Fixes the case Eugene flagged:
    for _ in [] do
        let x = 1
        x
- Fix wrong issue link (#5735 -> #5418) in code comment, release notes,
  and test annotations.
- Add regression tests for the let/use case (single + nested bindings).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

❗ Release notes required

You can open this PR in browser to add release notes: open in github.dev


✅ Found changes and release notes in following paths:

Warning

No PR link found in some release notes, please consider adding it.

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/11.0.100.md No current pull request URL (#19896) found, please consider adding it

@github-actions github-actions Bot added the AI-Tooling-Check-Bypassed Tooling check: non-fork PR, not diff-analyzed label Jun 5, 2026
@T-Gro T-Gro requested a review from abonie June 5, 2026 09:49
@T-Gro T-Gro enabled auto-merge (squash) June 5, 2026 09:49

@abonie abonie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI-generated review — verify before acting on findings.

|> typecheck
|> shouldFail
|> withSingleDiagnostic (Warning 20, Line 5, Col 5, Line 5, Col 10,
"The result of this expression has type 'int' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Test Coverage / LOW] The PR title and release note both say "let/use", but the two new tests only cover let bindings. The use path goes through the identical code (SynExpr.LetOrUse is one union case for both), so it works — but there is no regression guard for it.

Suggested addition:

// https://github.com/dotnet/fsharp/issues/5418
[<Fact>]
let ``Warn On Last Expression In For Loop - non-unit after use binding``() =
    FSharp """
type D() =
    interface System.IDisposable with
        member _.Dispose() = ()
    member _.Value = 1
for _ in [] do
    use d = new D()
    d.Value
    """
    |> typecheck
    |> shouldFail
    |> withSingleDiagnostic (Warning 20, Line 8, Col 5, Line 8, Col 12,
                             "The result of this expression has type 'int' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.")

@abonie abonie added the AI-reviewed PR reviewed by AI review council label Jun 8, 2026
@github-project-automation github-project-automation Bot moved this from New to In Progress in F# Compiler and Tooling Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-reviewed PR reviewed by AI review council AI-Tooling-Check-Bypassed Tooling check: non-fork PR, not diff-analyzed

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants